<!--
 * @Author: adolphus adolphus520@163.com
 * @Date: 2022-12-05 17:54:25
 * @LastEditors: 姚杰鸿
 * @LastEditTime: 2024-11-11 18:27:12
 * @Description: file content
-->
<template>
  <MdsAdmin v-if="show" @tabs="logKeepAliveCache">
    <router-view v-slot="{ Component, route }">
      <transition name="scale" mode="out-in">
        <keep-alive ref="keepAlive">
          <component :is="Component" :key="route.path" />
        </keep-alive>
      </transition>
    </router-view>
  </MdsAdmin>
</template>

<script lang="ts" setup>
import { onMounted, ref, watch } from "vue";
import { useRoute } from "vue-router";
import { useStore, initMenuItems } from "@/store";
import { ElLoading } from "element-plus";
import { MenuApi } from "@/apis/home";
import * as vars from "@/lib/vars";
import { initTheme } from "@/components/elm/ThemeSelect";

import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
import timezone from "dayjs/plugin/timezone";

initTheme();
const TmZone = "Asia/Shanghai",
  TmFormat = "YYYY-MM-DD HH:mm:ss";

dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.tz.setDefault(TmZone);

const show = ref(false),
  keepAlive = ref(),
  $store = useStore(),
  $route = useRoute();

const logKeepAliveCache = (d: any) => {
  const cache = keepAlive.value.$?.__v_cache;
  cache.forEach((value: any, key: any) => {
    let hv = false;
    for (let i = 0; i < d.length; i++) {
      const entry = d[i];
      if (entry.name == key) hv = true;
    }
    if (!hv) {
      cache.delete(key);
    }
  });
};

onMounted(async () => {
  const loading = ElLoading.service({
    lock: true,
    text: "Loading",
    background: "rgba(0, 0, 0, 0.7)",
  });

  MenuApi("", false, true)
    .then((res) => {
      if (
        200 == res.status &&
        200 == res.data.code &&
        res.data.data &&
        res.data.data.list
      ) {
        const menus = res.data.data.list;
        //TODO: 后端处理
        const curUser: vars.User = $store.getters.getCurUser;
        console.log(curUser);
        if (curUser.roles && 0 < curUser.roles.length) {
          let isAdmin: boolean = false;
          for (let i = 0; i < curUser.roles.length; i++) {
            const role = curUser.roles[i];
            if (role.code == vars.SU_ROLE) {
              isAdmin = true;
              break;
            }
          }
          if (isAdmin) {
            let hv: boolean = false;
            for (let i = 0; i < menus.length; i++) {
              const menu = menus[i];
              if (menu.name === vars.MENU_MG) {
                hv = true;
              }
            }
            if (!hv) {
              const date = new Date();
              menus.push({
                base_field: {
                  id: 0,
                  created_tm: dayjs(date).tz(TmZone).format(TmFormat),
                  modify_tm: dayjs(date).tz(TmZone).format(TmFormat),
                  version: 0,
                },
                code: "0",
                name: vars.MENU_MG,
                ordernn: 9999,
              });
            }
          }
        }
        $store.commit(initMenuItems, menus);
        loading.close();
        show.value = true;
      } else {
        ElMessage.error(res.data.msg);
      }
    })
    .catch((err) => {
      ElMessage.error(err.response.data.msg);
    });
});

watch($route, (nV, oV) => {
  console.log("......", nV, oV);
});
</script>
